import Vue from "vue";
import VueRouter, { Route, RouteConfig } from "vue-router";
import Home from "../views/Home.vue";
import LayoutView from "@/layouts/LayoutView.vue";
import { Position } from "vue-router/types/router";

Vue.use(VueRouter);

export const routes: Array<RouteConfig> = [
  {
    path: "/",
    name: "root",
    redirect: "/index/main",
    meta: {
      hidden: true,
    },
  },
  {
    path: "/index",
    name: "index",
    component: LayoutView,

    meta: {
      title: "Dashboard",
      icon: "dashboard",
      hidden: false,
    },
    children: [
      {
        path: "main",
        name: "Main",
        component: () => import("@/views/Home.vue"),
        meta: {
          title: "主控台",
          affix: true,
          cacheable: true,
        },
      },
      {
        path: "workplace",
        name: "WorkPlace",
        component: () => import("@/views/About.vue"),
        meta: {
          title: "工作台",
          cacheable: true,
        },
      },
    ],
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },

  {
    path: "/redirect",
    component: LayoutView,

    meta: {
      hidden: true,
    },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/RedirectView.vue"),
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/LoginView.vue"),
    meta: {
      hidden: true,
    },
  },
];

const createRouter = () =>
  new VueRouter({
    scrollBehavior: (
      to: Route,
      from: Route,
      savedPosition: void | Position
    ) => {
      return { x: 0, y: 0 };
    },
    mode: "hash",
    routes: routes,
  });
const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter();
  (router as any).matcher = (newRouter as any).matcher;
}
export default router;
